﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace DataShow.codeBLL
{
    public class ForecasterBLL
    {
        public bool AddItemSpanItem(DateTime time, string span, string clock, string sendpoint, string tableName, Dictionary<string, List<string>> allData)
        {
            UserAddData data;
            string value = "";
            //添加mint
            foreach (var item in allData)
            {
                value += (item.Key + ";" + item.Value[3] + "|");
            }
            value = value.Substring(0, value.Length - 1);
            data = new UserAddData() { Date = time.ToString("yyyyMMdd"), Clock = clock, SendPoint = sendpoint, Semitype = "Auto", Span = span, TableName = tableName, Type = "mint", Value = value };
            new codeDAL.ForecasterProcess().AddItem(data);

            //添加maxt
            value = "";
            foreach (var item in allData)
            {
                value += (item.Key + ";" + item.Value[2] + "|");
            }
            value = value.Substring(0, value.Length - 1);
            data = new UserAddData() { Date = time.ToString("yyyyMMdd"), Clock = clock, SendPoint = sendpoint, Semitype = "Auto", Span = span, TableName = tableName, Type = "maxt", Value = value };
            new codeDAL.ForecasterProcess().AddItem(data);

            //添加前12小时降雨
            string beginClock = span.Split('_')[0].Substring(2);
            string endClock = span.Split('_')[1]; //FD22_120
            string front = "FD" + beginClock + "_" + (Convert.ToInt32(endClock) - 12).ToString();
            value = "";
            foreach (var item in allData)
            {
                value += (item.Key + ";" + item.Value[0] + "|");
            }
            value = value.Substring(0, value.Length - 1);
            data = new UserAddData() { Date = time.ToString("yyyyMMdd"), Clock = clock, SendPoint = sendpoint, Semitype = "Auto", Span = front, TableName = tableName, Type = "wth", Value = value };
            new codeDAL.ForecasterProcess().AddItem(data);
            
            //添加后12小时
            string back = "FD" + (Convert.ToInt32(beginClock) + 12).ToString() + "_" + endClock;
            value = "";
            foreach (var item in allData)
            {
                value += (item.Key + ";" + item.Value[1] + "|");
            }
            value = value.Substring(0, value.Length - 1);
            data = new UserAddData() { Date = time.ToString("yyyyMMdd"), Clock = clock, SendPoint = sendpoint, Semitype = "Auto", Span = back, TableName = tableName, Type = "wth", Value = value };
            new codeDAL.ForecasterProcess().AddItem(data);
            
            //添加当天的
            value = "";
            foreach (var item in allData)
            {
                value += (item.Key + ";" + GetMax(float.Parse(item.Value[0]), float.Parse(item.Value[1])).ToString() + "|");
            }
            value = value.Substring(0, value.Length - 1);
            data = new UserAddData() { Date = time.ToString("yyyyMMdd"), Clock = clock, SendPoint = sendpoint, Semitype = "Auto", Span = span, TableName = tableName, Type = "wth", Value = value };
            new codeDAL.ForecasterProcess().AddItem(data);
            return true;
        }

        protected float[] LevList = { 20.0f, 18.0f, 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 13.0f, 14.0f, 21.0f, 26.0f, 8.0f, 15.0f, 22.0f, 27.0f, 9.0f, 16.0f, 23.0f, 28.0f, 10.0f, 17.0f, 24.0f, 11.0f, 25.0f, 12.0f };

        public int GetLevIndex(float lev)
        {
            for (int i = 0; i < LevList.Length - 1; i++)
            {
                if (LevList[i] == lev)
                {
                    return i;
                }
            }

            return 0;
        }

        public float GetMax(float one, float other)
        {
            if (one == other)
                return one;
            else if (GetLevIndex(one) > GetLevIndex(other))
            {
                return one;
            }
            else
            {
                return other;
            }
        }

        

        #region 得到所有预报员的id和name
        /// <summary>
        /// 得到所有预报员的id和name
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> ReadAllForecasters()
        {
            DataTable dt = new codeDAL.ForecasterProcess().ReadAllForecasters();
            Dictionary<string, string> lists = new Dictionary<string, string>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow item in dt.Rows)
                {
                    lists.Add(item["username"].ToString(), item["name"].ToString());
                }
            }
            return lists;
        }
        #endregion
    }
}